package com.github.diy;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

import java.util.Arrays;

/**
 * @author fatfish
 * @version 1.0
 * @date 2025/8/15 17:07
 */
@Aspect
public class AnnotationPointCut {
    @Before("execution(* com.github.service.UserService.*(..))")
    public void before(JoinPoint jp) {
        System.out.println("---------方法执行前---------");
        System.out.println("目标对象: " + jp.getTarget());
        System.out.println("拦截的方法: " + jp.getSignature().getName());
        System.out.println("方法所属类: " + jp.getSignature().getDeclaringTypeName());
        System.out.println("方法参数: " + Arrays.toString(jp.getArgs()));
    }

    @After("execution(* com.github.service.UserService.*(..))")
    public void after(JoinPoint jp) {
        System.out.println("---------方法执行后---------");
        System.out.println("方法 " + jp.getSignature().getName() + " 执行完毕。");
    }

    @Around("execution(* com.github.service.UserService.*(..))")
    public void around(ProceedingJoinPoint jp) {
        System.out.println("---------方法执行前（环绕通知）---------");
        try {
            // 执行目标方法
            Object proceed = jp.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        System.out.println("---------方法执行后（环绕通知）---------");
        Signature signature = jp.getSignature(); // 获得签名
        System.out.println("签名: " + signature);
        System.out.println("方法 " + jp.getSignature().getName() + " 执行完毕。");
        System.out.println("目标对象: " + jp.getTarget());
        System.out.println("方法所属类: " + jp.getSignature().getDeclaringTypeName());
        System.out.println("方法参数: " + Arrays.toString(jp.getArgs()));
        System.out.println("环绕通知结束。\n");
    }
}
